<?php
/*
 * @Author: zwx
 * @Date: 2024-05-30 12:05:58
 * @LastEditors: zwx
 * @LastEditTime: 2024-09-25 21:13:58
 * @Description: 
 */
declare(strict_types=1);

namespace app\apiadmin\command;

use app\apiadmin\controller\behavior\Invitation;
use app\apiadmin\controller\liveManager\LiveStatus;
use app\apiadmin\service\invitation\InvitationService;
use app\apiadmin\service\TCSS\TImService;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Db;

class shuashu_tuifeiTag extends Command
{

    protected function configure()
    {
        // 命令的名称及用途描述
        $this->setName('shuashu_tuifeiTag')->setDescription('刷私域退费的公域订单标签');
    }

    protected function execute(Input $input, Output $output)
    {
        // 你的后台运行代码
        $output->writeln("shuashu_tuifeiTag");
        // 这里可以放置你的长时间运行的代码

        self::shuashu_tuifeiTag_live();

        self::shuashu_tuifeiTag_XE();

    }



    /**
     * @author: zwx
     * @Description: 私域订单退费标签
     * @Date: 2024-07-22 22:03:00
     */
    public function shuashu_tuifeiTag_live()
    {

        $where = [];
        $where['xh_siyu_order.del'] = 0;
        $where[] = ['xh_siyu_order.state', 'in', [2, 3]];

        $siyuorder = db::table('xh_siyu_order')
            ->field('xh_siyu_order.id,xh_siyu_order.order_id,xh_siyu_order.pay_time,xh_siyu_order_selled.pay_time as refund_time')
            ->join('xh_siyu_order_selled', 'xh_siyu_order_selled.order_num = xh_siyu_order.order_num')
            ->where($where)->select()->toArray();

        dump('私域退单数----' . count($siyuorder));

        foreach ($siyuorder as $k => $v) {
            $this->updateTag($v, 1);
        }

    }

    /**
     * @author: zwx
     * @Description: 小鹅通订单退费标签
     * @Date: 2024-07-22 22:03:00
     */
    public function shuashu_tuifeiTag_XE()
    {

        $where = [];
        $where['xh_xiaoe_order.del'] = 0;
        $where[] = ['xh_xiaoe_order.refund_state', 'in', [2, 3]];

        $siyuorder = db::table('xh_xiaoe_order')
            ->field('xh_xiaoe_order.id,xh_xiaoe_order.xh_order_id as order_id,xh_xiaoe_order.pay_time,xh_xiaoe_order.refund_time')
            ->where($where)->select()->toArray();

        dump('小鹅通退单数----' . count($siyuorder));

        foreach ($siyuorder as $k => $v) {
            $this->updateTag($v, 2);
        }

    }


    private function updateTag($v, $flag)
    {

        $where = [];
        $where['id'] = $v['order_id'];

        $data = [];

        $gongyuorder = db::table('xh_order_1')->where($where)->find();
        if ($gongyuorder) {
            if ($gongyuorder['new_chengjiao_tag'] == 0) {
                $data['new_tuikuan_tag'] = 1;
                $data['new_tuikuan_tag_time'] = $v['pay_time'];
            }
            if ($gongyuorder['new_tuikuan_tag'] == 0) {
                $data['new_tuikuan_tag'] = 1;
                $data['new_tuikuan_tag_time'] = $v['refund_time'];
            }
            db::table('xh_order_1')->where($where)->update($data);

            //记录更改前的日志
            $data2 = [];
            $data2['siyu_id'] = $v['id'];
            $data2['order_id'] = $v['order_id'];
            $data2['chengjiao_tag'] = $gongyuorder['new_chengjiao_tag'];
            $data2['tuikuan_tag'] = $gongyuorder['new_tuikuan_tag'];
            $data2['flag'] = $flag;

            db::table('xh_shuashu_tag')->insertGetId($data2);

            dump($data2);
        } else {
            dump('未查询到对应公域订单');

            //记录更改前的日志
            $data2 = [];
            $data2['siyu_id'] = $v['id'];
            $data2['order_id'] = $v['order_id'];
            $data2['flag'] = $flag;
            $data2['type'] = 1;

            db::table('xh_shuashu_tag')->insertGetId($data2);
        }

    }





}